package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.Employment;
import com.example.result.Result;
import com.example.service.EmploymentService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 就业登记控制层
 * @date 2020-01-01
 */
@RestController
@RequestMapping("/api")
public class EmploymentController {

    @Autowired
    EmploymentService employmentService;

    // ==================================就业登记管理===============================================

    /**
     * @param json
     * @return 添加成功则返回成功提示信息，反之则提示添加失败信息
     * @describe 添加就业登记信息
     */
    @RequestMapping(value = "/employments", method = RequestMethod.POST)
    public Result addEmployment(@RequestBody String json) {

        // 将JSON数据转换为Employment对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Employment employment = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Employment.class);

        return employmentService.addEmployment(employment);
    }

    /**
     * @param json
     * @return 更新成功则返回成功提示信息，反之则提示更新失败信息
     * @describe 更新就业登记信息
     */
    @RequestMapping(value = "/employments", method = RequestMethod.PUT)
    public Result updateEmployment(@RequestBody String json) {
        // 将JSON数据转换为Employment对象实体
        JSONObject parseObject = JSON.parseObject(json);
        Employment employment = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), Employment.class);

        return employmentService.updateEmployment(employment);
    }

    /**
     * @param employmentId
     * @return 删除成功则返回成功提示信息，反之则提示删除失败信息
     * @describe 根据编号删除就业登记信息
     */
    @RequestMapping(value = "/employments/{employmentId}", method = RequestMethod.DELETE)
    public Result deleteEmploymentById(@PathVariable("employmentId") String employmentId) {

        return employmentService.deleteEmploymentById(employmentId);
    }

    /**
     * @return 以JSON格式返回所有学费信息
     * @describe 查询所有就业登记信息
     */
    @RequestMapping(value = "/employments", method = RequestMethod.GET)
    public Result queryAllEmployment() {

        return employmentService.queryAllEmployment();
    }

    /**
     * @param employmentId
     * @return 以JSON格式返回该就业登记信息
     * @describe 根据编号查询该就业登记信息
     */
    @RequestMapping(value = "/employments/{employmentId}", method = RequestMethod.GET)
    public Result queryEmploymentById(@PathVariable("employmentId") String employmentId) {

        return employmentService.queryEmploymentById(employmentId);
    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加就业登记信息
     */
    @RequestMapping(value = "/employments/excels", method = RequestMethod.POST)
    public Result addEmploymentByExcel(@RequestBody String json) {
        JSONObject parseObject = JSON.parseObject(json);
        List<Employment> list = JSON.parseArray(parseObject.getString("json"), Employment.class);

        return employmentService.addEmploymentByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据就业登记编号批量删除就业登记信息
     */
    @RequestMapping(value = "/employments/dels", method = RequestMethod.POST)
    public Result deleteEmploymentByIds(@RequestBody Map<String, String> Ids) {

        return employmentService.deleteEmploymentByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 就业登记信息模糊查询
     */
    @RequestMapping(value = "/employments/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {

        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return employmentService.serachByInfo(info);

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有就业登记和用户信息
     */
    @RequestMapping(value = "/employments/EmploymentAndUsers", method = RequestMethod.GET)
    public Result queryAllEmploymentAndUsersInfo() {
        return employmentService.queryAllEmploymentAndUsersInfo();
    }
}
